################################################################################
#* Licensed Materials - Property of ShaoYang                                   #
#*                                                                             #         
#* OCO Source Materials                                                        #          
#*                                                                             #                 
#* (C) Copyright ShaoYang Corp. 2015 All Rights Reserved                       #
#*                                                                             # 
#* The source code for this program is not published or other-                 #
#* wise divested of its trade secrets, irrespective of what has                #
#* been deposited.                                                             # 
################################################################################

#*******************************************************************************
# Author:yushaoyang@cn.ibm.com
# Createdate:Fri Jun  5 21:29:16 2015
# Lastupd:Fri Jun  5 21:29:16 2015

# Description
# Updatelog:
# 1. "Init"
#*******************************************************************************

module Query
  module ExportService
    class QueryService
      def self.init_service dbcfgpath
        ymlpath=File.expand_path('../db/mongoid.yml', __FILE__)
        Mongoid.load!(ymlpath,:production)
      end
      
      def doGet req
          log.debug req
          log.debug req[:body]
          log.debug req[:params]
          log.debug req[:qparams]
          log.debug req[:action]
          log.debug req[:objname]
          log.debug req[:qparams]["patientId"]
          log.debug req[:qparams]["stime"]
          log.debug req[:qparams]["etime"]

          if req[:objname] != nil
            case req[:objname]
            when "findLastByPatientID"
              if req[:qparams]["patientId"] != nil
                #RawData.where(:patientId => req[:qparams][:patientId]).last
                result=RawData.where(:patientId => req[:qparams]["patientId"]).last,200
              else
                result="Invalid id",400
              end
            when "findByTime"
              if !_hasValue(req[:qparams]["stime"]) && !_hasValue(req[:qparams]["etime"])
                #have two time
                log.debug "**DB return**"
                log.debug RawData.where(:stime.gte =>Time.new(req[:qparams]["stime"]), :etime.lte => Time.new(req[:qparams]["etime"]))
                result="between",200
              elsif !_hasValue(req[:qparams]["stime"]) && _hasValue(req[:qparams]["etime"])
                #only start time
                log.debug "**DB return**"
                log.debug RawData.where(:stime.gte => Time.new(req[:qparams]["stime"]))
                result="start",200
              elsif _hasValue(req[:qparams]["stime"])  && !_hasValue(req[:qparams]["etime"])
                #only end time
                log.debug "**DB return**"
                log.debug RawData.where(:etime.lte => Time.new(req[:qparams]["etime"]))
                result="end",200
              else
                result="At least give one time arguments",400
              end
            else 
              result="nothing happens",200
            end
          else
            result="Invalid URL",400
          end
      end

      def doPost req  # For Create
          log.debug req
          log.debug req[:body]
          log.debug req[:params]
          log.debug req[:qparams]
          log.debug req[:action]
          log.debug req[:objname]
          result="papapa",200
      end

      def doPut req # For Update
        loadog.debug req
          log.debug req[:body]
          log.debug req[:params]
          log.debug req[:qparams]
          log.debug req[:action]
          log.debug req[:objname]
          result="papapa",200
      end

      def doDelete req # For Delete
          log.debug req
          log.debug req[:body]
          log.debug req[:params]
          log.debug req[:qparams]
          log.debug req[:action]
          log.debug req[:objname]
          result="papapa",200
      end
      
      def _hasValue var
        if var.nil? || var.empty?
          return true
        else
          return false
        end
      end
    end
  end
end
